package com.pcbly.web.components.utils.converters;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pcbly.web.components.utils.TimeOp;



public class MyDateConverter implements Converter {
	protected static final Log logger = LogFactory
			.getLog(MyDateConverter.class);
	public MyDateConverter(){
		
	}
	@SuppressWarnings("unchecked")
	public Object convert(Class type, Object value) {
		if (type.equals(java.util.Date.class)) {
			if (value != null) {
				if (value.getClass().equals(java.lang.String.class))
					return TimeOp.convertStrToDateTime((String) value);
				if (value.getClass().equals(java.util.Date.class))
					return value;
				logger
						.warn("\u8C03\u7528DateConvert\u65F6\u6CA1\u6709\u5339\u914D\u7684\u503C\u7C7B\u578B:"
								+ value.getClass());
			}
		} else if (type.equals(java.sql.Date.class)) {
			if (value != null) {
				if (value.getClass().equals(java.lang.String.class))
					return new Date(TimeOp.convertStrToDateTime((String) value)
							.getTime());
				if (value.getClass().equals(java.sql.Date.class))
					return value;
				logger
						.warn("\u8C03\u7528DateConvert\u65F6\u6CA1\u6709\u5339\u914D\u7684\u503C\u7C7B\u578B:"
								+ value.getClass());
			}
		} else if (type.equals(java.sql.Timestamp.class) && value != null) {
			if (value.getClass().equals(java.lang.String.class))
				return new Timestamp(TimeOp
						.convertStrToDateTime((String) value).getTime());
			if (value.getClass().equals(java.sql.Timestamp.class))
				return value;
			logger
					.warn("\u8C03\u7528DateConvert\u65F6\u6CA1\u6709\u5339\u914D\u7684\u503C\u7C7B\u578B:"
							+ value.getClass());
		}
		return null;
	}

}
